package org.springblade.modules.repair.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.repair.entity.MaintainMsgRead;
import org.springblade.modules.repair.enums.MsgStatusEnum;
import org.springblade.modules.repair.enums.MsgTypeEnum;
import org.springblade.modules.repair.service.MaintainMsgReadService;
import org.springblade.modules.repair.vo.MaintainMsgVO;
import org.springblade.modules.repair.vo.MsgTypeVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IRoleMenuService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.repair.entity.MaintainMsg;
import org.springblade.modules.repair.mapper.MaintainMsgMapper;
import org.springblade.modules.repair.service.MaintainMsgService;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class MaintainMsgServiceImpl extends BaseServiceImpl<MaintainMsgMapper, MaintainMsg> implements MaintainMsgService{

	private final IUserService userService;
	private final IRoleMenuService roleMenuService;
	private final MaintainMsgReadService msgReadService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void sendWebMsg(MaintainMsg msg, List<Long> userIds) {
		super.save(msg);
		if(ObjectUtil.isNotEmpty(userIds)){
			List<MaintainMsgRead> msgReads = new ArrayList<>();
			userIds.forEach(e -> {
				MaintainMsgRead msgRead = new MaintainMsgRead();
				msgRead.setMsgId(msg.getId());
				msgRead.setUserId(e);
				msgReads.add(msgRead);
			});
			msgReadService.saveBatch(msgReads);
//				MaintainMsgVO msgVO = baseMapper.selectMsgById(msg.getId());
//				delayedSend.sendMsg(System.currentTimeMillis(), JSONObject.toJSONString(msgVO));
		}
	}

	@Override
	public List<MsgTypeVO> listMsgType() {
		List<MsgTypeVO> msgTypeVOS = baseMapper.listMsgTye(AuthUtil.getUserId());
		msgTypeVOS.forEach(e -> {
			e.setTypeName(MsgTypeEnum.getName(e.getTypeId()));
		});
		return msgTypeVOS;
	}

	@Override
	public IPage<MaintainMsgVO> selectMsgPage(String keyword, String typeIds, Integer status, Integer bType, IPage<MaintainMsgVO> page) {
		return page.setRecords(baseMapper.selectMsgPage(keyword,typeIds, status,bType,AuthUtil.getUserId(), page));
	}

	@Override
	public Boolean readBath(List<Long> typeIds, Long userId) {
		List<Long> msgIds = super.list(Wrappers.<MaintainMsg>lambdaQuery().in(MaintainMsg::getType,typeIds)).stream().map(BaseEntity::getId).collect(Collectors.toList());
		return msgReadService.update(Wrappers.<MaintainMsgRead>lambdaUpdate().set(MaintainMsgRead::getStatus, MsgStatusEnum.UNREAD.getId()).in(MaintainMsgRead::getMsgId,msgIds).eq(MaintainMsgRead::getUserId,userId));
	}

	@Override
	public Boolean read(Long id, Long userId) {
		return msgReadService.update(Wrappers.<MaintainMsgRead>lambdaUpdate().set(MaintainMsgRead::getStatus, MsgStatusEnum.UNREAD.getId()).set(MaintainMsgRead::getUpdateTime, DateUtil.date()).eq(MaintainMsgRead::getMsgId,id).eq(MaintainMsgRead::getUserId,userId));
	}

	@Override
	public Long sendMsg(MaintainMsg msg, List<Long> userIds, String tenantId) {
		msg.setTenantId(tenantId);
		super.save(msg);
		if(ObjectUtil.isNotEmpty(userIds)){
			List<MaintainMsgRead> msgReads = new ArrayList<>();
			userIds.forEach(e -> {
				MaintainMsgRead msgRead = new MaintainMsgRead();
				msgRead.setMsgId(msg.getId());
				msgRead.setUserId(e);
				msgRead.setTenantId(tenantId);
				msgReads.add(msgRead);
			});
			msgReadService.saveBatch(msgReads);
		}
		return msg.getId();
	}

	@Override
	public Boolean readAll(Long userId) {
		return msgReadService.update(Wrappers.<MaintainMsgRead>lambdaUpdate().set(MaintainMsgRead::getStatus, MsgStatusEnum.UNREAD.getId()).set(MaintainMsgRead::getUpdateTime, DateUtil.date()).eq(MaintainMsgRead::getUserId,userId));
	}
}
